<template>
	<xn-form-container
		title="发送信息"
		:width="550"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" :label-col="labelCol">
			<a-form-item label="账号类型：" name="type">
				<a-select v-model:value="formData.type" placeholder="请选择账号类型" :options="typeOptions" />
			</a-form-item>
			<a-form-item label="发送账号：" name="codeKey">
				<a-input v-model:value="formData.codeKey" placeholder="请输入发送账号" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">发送</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="CodeForm">
	import { required } from '@/utils/formRules'
	import tool from '@/utils/tool'
	import CodeApi from '@/api/set/CodeApi'
	// 默认是关闭状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	// 表单固定label实现
	const labelCol = ref({
		style: {
			width: '100px'
		}
	})
	// 打开抽屉
	const onOpen = () => {
		visible.value = true
		formData.value = {
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible.value = false
	}

	// 默认要校验的
	const formRules = {
		codeKey: [required('请输入发送账号')],
		type: [required('请选择账号类型')]
	}

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			CodeApi
				.send(formData.value)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 分类
	const typeOptions = tool.dictList('VERIFICATION_TYPE')

	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
